function e(e) {
    if (Array.isArray(e)) {
        for (var r = 0, t = Array(e.length); r < e.length; r++) t[r] = e[r];
        return t;
    }
    return Array.from(e);
}

function r(e, r) {
    if (!(e instanceof r)) throw new TypeError("Cannot call a class as a function");
}

function t(e) {
    return Object.keys(e).sort().map(function(r) {
        return r + "=" + e[r];
    }).join("&");
}

Object.defineProperty(exports, "__esModule", {
    value: !0
});

var n = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(e) {
    return typeof e;
} : function(e) {
    return e && "function" == typeof Symbol && e.constructor === Symbol && e !== Symbol.prototype ? "symbol" : typeof e;
}, o = Object.assign || function(e) {
    for (var r = 1; r < arguments.length; r++) {
        var t = arguments[r];
        for (var n in t) Object.prototype.hasOwnProperty.call(t, n) && (e[n] = t[n]);
    }
    return e;
}, a = function() {
    function e(e, r) {
        var t = [], n = !0, o = !1, a = void 0;
        try {
            for (var i, u = e[Symbol.iterator](); !(n = (i = u.next()).done) && (t.push(i.value), 
            !r || t.length !== r); n = !0) ;
        } catch (e) {
            o = !0, a = e;
        } finally {
            try {
                !n && u.return && u.return();
            } finally {
                if (o) throw a;
            }
        }
        return t;
    }
    return function(r, t) {
        if (Array.isArray(r)) return r;
        if (Symbol.iterator in Object(r)) return e(r, t);
        throw new TypeError("Invalid attempt to destructure non-iterable instance");
    };
}(), i = function() {
    function e(e, r) {
        for (var t = 0; t < r.length; t++) {
            var n = r[t];
            n.enumerable = n.enumerable || !1, n.configurable = !0, "value" in n && (n.writable = !0), 
            Object.defineProperty(e, n.key, n);
        }
    }
    return function(r, t, n) {
        return t && e(r.prototype, t), n && e(r, n), r;
    };
}();

exports.serialize = t;

var u = function(e) {
    if (e && e.__esModule) return e;
    var r = {};
    if (null != e) for (var t in e) Object.prototype.hasOwnProperty.call(e, t) && (r[t] = e[t]);
    return r.default = e, r;
}(require("./request")), s = function(e) {
    return e && e.__esModule ? e : {
        default: e
    };
}(require("./util")), c = function() {
    function c(e, t) {
        r(this, c), this.$register(e, t), this.$requestInterceptors = [], this.$responseInterceptors = [];
    }
    return i(c, null, [ {
        key: "concatInterceptor",
        value: function() {
            var e = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : [], r = arguments[1];
            return r ? e.concat(r) : e;
        }
    } ]), i(c, [ {
        key: "$register",
        value: function(e, r) {
            Object.assign(this, this.$generate(e, r));
        }
    }, {
        key: "$generate",
        value: function() {
            var e = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {}, r = this, i = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : "", s = arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : "/", l = {};
            return Object.keys(e).forEach(function(f) {
                var p = e[f];
                if ("string" == typeof p && (p = [ p ]), Array.isArray(p)) {
                    var v = a(p, 3), y = v[0], h = void 0 === y ? "get" : y, d = v[1], b = void 0 === d ? {} : d, m = v[2], g = void 0 === m ? {} : m, $ = "" + (b.host ? b.host : i) + s + f;
                    l[f] = function() {
                        var e = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {}, n = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : {};
                        (n = o({}, b, n)).isData && (e = {
                            data: e
                        }), (e = o({}, g, e, {
                            url: $,
                            method: h
                        })).data = Object.assign({}, g.data, e.data), e.param && (e.param = Object.assign({}, g.param, e.param), 
                        e.url += "?" + t(e.param));
                        var a = c.concatInterceptor(r.$requestInterceptors, n.requestInterceptors), i = c.concatInterceptor(r.$responseInterceptors, n.responseInterceptors);
                        return r.$runResolvers(a, e).then(function(e) {
                            e.method = e.method.toLowerCase ? e.method.toLowerCase() : "";
                            var t = "upload" === e.method ? u.upload(e) : u.request(e);
                            return t.catch(r.$errorHandler), t.then(function(e) {
                                return r.$runResolvers(i, e);
                            });
                        });
                    }, l[f].url = $, b.alias && (l[b.alias] = l[f]);
                } else "object" === (void 0 === p ? "undefined" : n(p)) && (l[f] = r.$generate(p, i, "" + s + f + "/"));
            }), l;
        }
    }, {
        key: "$runResolvers",
        value: function(r, t) {
            var n = (r = [].concat(e(r)))[0];
            return n ? (r.shift(), n(t).then(this.$runResolvers.bind(this, r))) : Promise.resolve(t);
        }
    }, {
        key: "$errorHandler",
        value: function(e) {
            return s.default.requestFailTip(e), Promise.reject(e);
        }
    } ]), c;
}();

exports.default = c;